//
//  ArcProgressBarView.swift
//  DaRings
//
//  Created by 尹琼 on 2022/11/8.
//

import UIKit
import SnapKit
import MYModule


open class ArcProgressBarView: View {
    
    public let stepsView = ArcClorsView()
    
    public let caloriesView = ArcClorsView()
    
    public let durationView = ArcClorsView()
    
    public var hiddenDataProgress: Bool = false {
        didSet {
            
            stepsView.arcView.isHidden = hiddenDataProgress
            caloriesView.arcView.isHidden = hiddenDataProgress
            durationView.arcView.isHidden = hiddenDataProgress
            
        }
    }
    
    public var spaceWidth: CGFloat = 1.5 {
        didSet {
            updateLayout()
        }
    }
    
    public var lineWidth: CGFloat = 7.0 {
        
        didSet {
            stepsView.lineWidth = lineWidth
            caloriesView.lineWidth = lineWidth
            durationView.lineWidth = lineWidth
            
            updateLayout()
            
        }
        
    }
    

    
    open override func makeUI() {
        super.makeUI()
        
        stepsView.arcView.strokeColor = UIColor.Step.theme
        caloriesView.arcView.strokeColor = UIColor.subTheme
        durationView.arcView.strokeColor = UIColor.theme
        
        addSubview(stepsView)
        addSubview(caloriesView)
        addSubview(durationView)
        
        stepsView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        caloriesView.snp.makeConstraints { make in
            
            let spacing = stepsView.bgArcView.lineWidth + spaceWidth
            make.centerX.centerY.equalToSuperview()
            make.size.equalTo(stepsView.snp.size).offset(-2 * spacing)
            
        }
        
        durationView.snp.makeConstraints { make in
            
            let spacing = caloriesView.bgArcView.lineWidth + spaceWidth
            make.centerX.centerY.equalToSuperview()
            make.size.equalTo(caloriesView.snp.size).offset(-2 * spacing)
        }
    }
    
    func updateLayout() {
        
        caloriesView.snp.updateConstraints { make in
            
            let spacing = stepsView.bgArcView.lineWidth + spaceWidth
            make.size.equalTo(stepsView.snp.size).offset(-2 * spacing)
            
        }
        
        durationView.snp.updateConstraints { make in
            
            let spacing = caloriesView.bgArcView.lineWidth + spaceWidth
            make.size.equalTo(caloriesView.snp.size).offset(-2 * spacing)
        }
        
    }

}

extension ArcProgressBarView {
    
    open class ArcClorsView: View {
        
       
        public var lineWidth: CGFloat = 7.0 {
            
            didSet {
                arcView.lineWidth = lineWidth
                bgArcView.lineWidth = lineWidth
            }
            
        }
        
        var progress: CGFloat? {
            
            didSet {
                
                guard let progress = progress else {
                    arcView.proportion = 0.0
                    return
                }

                var p = progress
                if progress > 0.97 && progress < 1.0 {
                    p = 0.97
                }
                
                arcView.proportion = p
            }
            
        }
        
        public let bgArcView = ArcView()
        
        public let arcView = ArcView()
        
        open override func makeUI() {
            super.makeUI()
            
            bgArcView.proportion = 1.0
            addSubview(bgArcView)
            addSubview(arcView)
            
            bgArcView.snp.makeConstraints { make in
                make.edges.equalToSuperview()
            }
            
            arcView.snp.makeConstraints { make in
                make.edges.equalToSuperview()
            }
        }
        
    }
    
}

extension ArcProgressBarView {
    
    open class ArcView: View {
        
        
        public var lineWidth: CGFloat = 7.0 {
            
            didSet {
                shapeLayer.lineWidth = lineWidth
            }
            
        }
        public var strokeColor: UIColor = UIColor.assist_A9A9A9.withAlphaComponent(0.3) {
            
            didSet {
                shapeLayer.strokeColor = strokeColor.cgColor
            }
            
        }
        
        private(set) var isEmpty: Bool = false
        
        
        public var proportion: CGFloat = 1.0 {
            
            didSet {
                shapeLayer.removeFromSuperlayer()
                self.setNeedsDisplay()
        
            }
            
        }
        
      
        public let shapeLayer = CAShapeLayer()
        
        
        open override func makeUI() {
            super.makeUI()
            
            self.backgroundColor = .clear
            shapeLayer.backgroundColor = UIColor.clear.cgColor
            shapeLayer.fillColor = UIColor.clear.cgColor
            shapeLayer.lineCap = .round
            
            shapeLayer.lineWidth = lineWidth
            shapeLayer.strokeColor = strokeColor.cgColor
            
            layer.addSublayer(shapeLayer)
            
            shapeLayer.position = self.center
            shapeLayer.bounds = self.frame
            
        }
        
        var path: UIBezierPath {
            
            let radius = (self.frame.width > self.frame.height ? self.frame.height/2.0 : self.frame.width/2.0)
            
            let center = CGPoint(x: self.frame.width/2.0, y: self.frame.height/2.0)
            
            var proportion = self.proportion
            
            if proportion <= 0.01 {
               
                proportion = 0.01
                isEmpty = true
            } else {
                isEmpty = false
            }
            
            
            let path = UIBezierPath(arcCenter: center, radius: radius, startAngle: -CGFloat.pi/2.0, endAngle: CGFloat.pi * 2.0 * proportion - CGFloat.pi/2.0, clockwise: true)
            
            return path
        }
        

        
        open override func layoutSubviews() {
            super.layoutSubviews()
            
            if ((self.layer.sublayers?.contains(shapeLayer)) != nil) {
                return
            }
            self.layer.addSublayer(shapeLayer)
            shapeLayer.path = self.path.cgPath
        }
        
        
    }
    
}

extension ArcProgressBarView {
    
    func arcPoint(center: CGPoint, angle: CGFloat, radius: CGFloat) ->CGPoint {
        
        let x2 = radius * CGFloat(cosf(Float(angle * CGFloat.pi/180.0)))
        let y2 = radius * CGFloat(sinf(Float(angle * CGFloat.pi/180.0)))
        
        return CGPoint(x: center.x + x2, y: center.y - y2)
        
    }
   
}
